package game

import (
	"fmt"
	"gosum/server/src/csv"
	"regexp"
	"sync"
	"time"
)

//单例

var mangerBanWord *ManageBanWords
var once sync.Once

type ManageBanWords struct {
	BanWordsBase  []string
	BanWordsExtra []string
	Test          map[int]int
	MsgChan       chan int
}

func GetManageBanWord() *ManageBanWords {
	once.Do(func() {
		if mangerBanWord == nil {
			mangerBanWord = new(ManageBanWords)
			mangerBanWord.BanWordsBase = []string{"外挂", "工具"}
			mangerBanWord.BanWordsExtra = []string{"原神"}
			mangerBanWord.Test = make(map[int]int)
			mangerBanWord.MsgChan = make(chan int)
		}
	})
	return mangerBanWord
}

func (self *ManageBanWords) IsBanWord(txt string) bool {

	for _, v := range self.BanWordsBase {
		match, _ := regexp.MatchString(v, txt)
		//fmt.Println(match, v)
		if match {
			return match
		}
	}

	for _, v := range self.BanWordsExtra {
		match, _ := regexp.MatchString(v, txt)
		fmt.Println(match, v)
		if match {
			return match
		}
	}
	return false
}

func (self *ManageBanWords) Run() {
	server.AddGo()
	self.BanWordsBase = csv.GetBanWordsBase()
	ticket := time.NewTicker(time.Second * 1)
	for {
		select {
		case <-ticket.C:
			//todo channel 关闭
			if time.Now().Unix()%10 == 0 {
				fmt.Println("更新词库")
				GetServer().UpdateBanWords(self.BanWordsBase)
			} else {
				//fmt.Println("____")
			}
		case _, ok := <-self.MsgChan:
			if !ok {
				server.GoDone()
				return
			}
		}
	}

}

func (self *ManageBanWords) Close() {
	close(self.MsgChan)
}
